//
//  APWebViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/1.
//

#import "APWebViewController.h"
#import "APNavigationBar.h"

@interface APWebViewController ()<APNavigationBarProtocol,WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) APNavigationBar *naviBarView;
@end

@implementation APWebViewController

- (APNavigationBar *)naviBarView{
    if (!_naviBarView) {
        _naviBarView = [[APNavigationBar alloc] init];
        _naviBarView.delegate = self;
    }
    return _naviBarView;
}

- (WKWebView *)webView{
    if (!_webView) {
//        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
//        config.preferences.javaScriptEnabled = YES;
//        config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
//        config.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
        _webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _webView.allowsLinkPreview = false;
        _webView.scrollView.directionalLockEnabled = YES;
        _webView.scrollView.bounces = NO;
        _webView.navigationDelegate = self;
        _webView.backgroundColor = [UIColor whiteColor];
//        [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
//        [_webView addObserver:self forKeyPath:@"URL" options:NSKeyValueObservingOptionNew context:nil];
    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.naviBarView.titleLabel.text = self.titleString;
    [self.view addSubview:self.naviBarView];
    [self.naviBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
    }];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.naviBarView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    [self loadWebView];
    
    if ([self.urlString isEqualToString:Term_Use__URL]) {
        [APLogManager statisticWithPage:APPage_terms];
    }else if ([self.urlString isEqualToString:Privacy_Policy_URL]){
        [APLogManager statisticWithPage:APPage_privacy];
    }
}


- (void)loadWebView{
    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (void)backButtonDidClick{
    [self dismissViewControllerAnimated:true completion:nil];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationFullScreen;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}

@end
